package com.hongdee.crm.role.service.impl;

import com.hongdee.common.dao.IHibernateDao;
import com.hongdee.crm.role.dao.ITcrmRoleDao;
import com.hongdee.crm.role.entity.TCrmCarRole;
import com.hongdee.crm.role.service.ITcrmRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
/**
 * Created by dell on 2017/5/4.
 */
public class TcrmRoleServiceimpl implements ITcrmRoleService {
    @Autowired
    private  ITcrmRoleDao iTcrmRoleDao;
    private  String tabName="t_car_crm_role";
    private  String tapName="t_crm_car_template";
    private  String tacName="t_bc_role";
     private List<String> deptList;
    @Autowired
    private IHibernateDao iHibernateDao;


    /*
* 停用启用
* */
    public void update(String id, String status) throws Exception {
        String hql = "update TCrmCarRole set status = :status where id = :id  ";
        Map params = new HashMap();
        params.put("id", id);
        params.put("status", status);
        System.out.print("status的值是!!!!!!"+status);
        this.iTcrmRoleDao.update(hql, params);
    }

/*
* 判断角色是否存在
* **/
    private boolean isExist(String templateName ,int type,String id) throws Exception {
        String sql = "select * from " + tabName + " where role_name = ? ";
        List param = new ArrayList();
        param.add(templateName);
        if (1 == type) {
            sql += " and  id != ? ";
            param.add(id);
        }
        Map map = this.iTcrmRoleDao.page(sql, param.toArray(), null, null);
        return Integer.valueOf(map.get("total").toString()) > 0;
    }
    /*
*   更改数据
* */
    public Map info(String id) throws Exception {
        String sql = "select * from " + tabName + " where id = ? ";
     System.out.print("info里deid和sql"+id);
        return this.iTcrmRoleDao.info(sql, id);
    }

    /*
    * 分页查询
    * */
    public Map newPage(String start, String limit, String templateName) throws Exception{
        String sql = "select a.id,a.template_name,a.status,a.role_name, b.template_name as tn, c.r_name FROM t_car_crm_role a LEFT JOIN t_crm_car_template b ON a.template_name = b.id LEFT JOIN t_bc_role c ON a.role_name = c.id  where 1=1";
        List<Object> params = new ArrayList();
        System.out.println(params);
        if (!StringUtils.isEmpty(templateName)) {
                System.out.print("11111111111111111");
            /*
            * 模糊查询
            * */
            sql += " and  b.template_name like ? or c.r_name like ?";
            params.add("%" + templateName + "%");
            params.add("%" + templateName + "%");
            System.out.print("params里面的东西是:"+params);
        }
        return this.iTcrmRoleDao.page(sql,params.toArray(),start,limit);
    }

    /*
    * 保存，更新
    * **/
    public TCrmCarRole saveOrUpdate(TCrmCarRole tem) throws Exception {
        if (null == tem.getId() || StringUtils.isEmpty(tem.getId())) {
            if (isExist(tem.getRoleName(),0,null)){
                tem.setStatus("0");
                throw new Exception("角色已绑定模板！");
            }
            this.iTcrmRoleDao.saveOrUpdate(tem);
        } else {
            if (isExist( tem.getRoleName(),1, tem.getId())) {
                throw new Exception("角色已绑定模板！");
            }
//            Map oldMap = this.iTcrmTemplateDao.info(tabName, "id", ref.getId());
            this.iTcrmRoleDao.saveOrUpdate(tem);
        }
        //修正下一级编码
        return  tem;
}
    /*
    * 模板列表下拉框
    *
    * */
    @Override
    public List<Map> combo() throws Exception {
        String sql = " select * from " + tapName + " where template_status = '1' ";
        List<Map<String, Object>> mapList = this.iTcrmRoleDao.list(sql);
        List<Map> resList = new ArrayList<Map>();
        Map temp;
        for (Map map : mapList) {
            temp = new HashMap();
            temp.put("label", map.get("templateName"));
            temp.put("value", map.get("id"));
            resList.add(temp);
        }
        return resList;
    }

    /**
     * 根据角色信息 获得角色所对应的模板信息
     * @param roleId
     * @return
     * @throws Exception
     */
    @Override
    public Map findTemplateByRoleId(String roleId) throws Exception {
        String sql="select temp.* from t_car_crm_role role,t_crm_car_template temp where role.template_name=temp.id  and role.role_name=? limit 0,1";
        return iTcrmRoleDao.info(sql,roleId);
    }


//    return resList;
//   }
}
